package com.lishe.lishe.lishe.module.mall.adapter;

import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerGriadViewItemDecoration;
import com.lishe.lishe.lishe.module.mall.bean.MallObject;
import com.lishe.lishe.lishe.utils.Utils;

import java.util.List;

/**
 * Created by shino on 2017/11/6.
 * 商城首页
 */

public class MallRecyClerViewAdapter extends RecyclerviewBaseAdapter {
    private List<MallObject> data;
    private Activity context;
    private int TYPE_IMAGE = 1;//活动image
    private int TYPE_CLASSIFY = 2;//导航分类
    public static final int TYPE_VALUSE = 3;//超值专区
    private int TYPE_ACTIVITY = 4;//精选活动
    private int TYPE_LOVE = 5;//猜你喜欢
    RecyclerGriadViewItemDecoration decoraton;

    public MallRecyClerViewAdapter(Activity context, List<MallObject> data) {
        this.data = data;
        this.context = context;
        decoraton = new RecyclerGriadViewItemDecoration(Utils.dip2px(context, 8), 0, 0);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        RecyclerView.LayoutParams params = new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (viewType == TYPE_IMAGE) {//活动主题
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview, null);
            view.setLayoutParams(params);
            return new MallRecyClerViewHolder(view);
        } else if (viewType == TYPE_CLASSIFY) {//导航分类入口
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview, null);
            view.setLayoutParams(params);
            return new MallRecyClerViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_title_recyclerview, parent,false);
            view.setLayoutParams(params);
            return new TitleViewRecyClerViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder0, int position) {
        int viewType = getItemViewTypeMy(position);

        MallObject mallObject = data.get(position);
        if (viewType == TYPE_IMAGE) {//活动主题
            MallRecyClerViewHolder holder = (MallRecyClerViewHolder) holder0;
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
            holder.recyclerView.setAdapter(mallObject.getAdapter());
        } else if (viewType == TYPE_CLASSIFY) {//导航分类入口
            MallRecyClerViewHolder holder = (MallRecyClerViewHolder) holder0;
            if (mallObject.getItemObject().size() < 5) {
                holder.recyclerView.setLayoutManager(new GridLayoutManager(context, mallObject.getItemObject().size()));
            } else {
                holder.recyclerView.setLayoutManager(new GridLayoutManager(context, 5));
            }
            holder.recyclerView.setAdapter(mallObject.getAdapter());
        } else if (viewType == TYPE_VALUSE) {//超值专区
            TitleViewRecyClerViewHolder holder = (TitleViewRecyClerViewHolder) holder0;
            holder.title.setText(mallObject.getTitle());
            ImageLoaderUtils.displayImage(context,mallObject.getIcon_url(), holder.image, R.mipmap.defalut_img);
            GridLayoutManager layoutManager=new GridLayoutManager(context, 2);
            layoutManager.setAutoMeasureEnabled(true);
//            holder.recyclerView.setLayoutManager(new GridLayoutManager(context, 2));
            holder.recyclerView.removeItemDecoration(decoraton);
            holder.recyclerView.addItemDecoration(decoraton);
            holder.recyclerView.setLayoutManager(layoutManager);
            holder.recyclerView.setAdapter(mallObject.getAdapter());
        } else if (viewType == TYPE_ACTIVITY) {//精选活动
            TitleViewRecyClerViewHolder holder = (TitleViewRecyClerViewHolder) holder0;
            holder.title.setText(mallObject.getTitle());
            ImageLoaderUtils.displayImage(context,mallObject.getIcon_url(), holder.image, R.mipmap.defalut_img);
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
            holder.recyclerView.setAdapter(mallObject.getAdapter());
        } else if (viewType == TYPE_LOVE) {//猜你喜欢
            TitleViewRecyClerViewHolder holder = (TitleViewRecyClerViewHolder) holder0;
            holder.title.setText(mallObject.getTitle());
            ImageLoaderUtils.displayImage(context,mallObject.getIcon_url(), holder.image, R.mipmap.defalut_img);
            holder.recyclerView.setLayoutManager(new GridLayoutManager(context, 2));
            holder.recyclerView.removeItemDecoration(decoraton);
            holder.recyclerView.addItemDecoration(decoraton);
            holder.recyclerView.setAdapter(mallObject.getAdapter());
        }

    }

    @Override
    public int getItemCountMy() {
        return data == null ? 0 : data.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
//        Log.e("类型",data.get(position).getStyle()+"--"+position);
        if (data.get(position).getStyle().equals("recommendStyle")) {//活动主题
            return TYPE_IMAGE;
        }
        if (data.get(position).getStyle().equals("cateStyle")) {//导航栏
            return TYPE_CLASSIFY;
        }
        if (data.get(position).getStyle().equals("valueStyle")) {//超值专区
            return TYPE_VALUSE;
        }
        if (data.get(position).getStyle().equals("activityStyle")) {//精选活动
            return TYPE_ACTIVITY;
        }
        if (data.get(position).getStyle().equals("loveStyle")) {//猜你喜欢
            return TYPE_LOVE;
        }
        return 0;
    }

    //其他只有recyclerview
    class MallRecyClerViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;

        public MallRecyClerViewHolder(View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.item_recyclerView);
        }
    }

    //专题活动
    class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView image;

        public ImageViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
        }
    }

    //带标题
    class TitleViewRecyClerViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;
        ImageView image;
        TextView title;

        public TitleViewRecyClerViewHolder(View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.item_recyclerView);
            image = itemView.findViewById(R.id.icon);
            title = itemView.findViewById(R.id.title);
        }
    }
}
